import { _decorator, Color, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { DevelopState, Soldier } from '../entity/Soldier';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { DevelopDetail } from './DevelopDetail';
import { Events } from '../entity/Events';
import { Utils } from '../common/Utils';
import { Tech, TechType } from '../entity/Tech';
import { DevelopTech } from './DevelopTech';
import { Developing } from './Developing';
import { UpgradeTech } from './UpgradeTech';
import { Upgrading } from './Upgrading';
import { UserInfo } from '../common/UserInfo';
import { UpgradeTechBuilding } from './UpgradeTechBuilding';
import { UpgradingBuilding } from './UpgradingBuilding';
import { AudioManager, Mp3Enum } from '../common/AudioManager';

const { ccclass, property } = _decorator;

@ccclass('DevelopIndexItem')
export class DevelopIndexItem extends Component {
    @property(Sprite)
    border: Sprite
    @property(Sprite)
    cover: Sprite

    @property(Label)
    develop: Label
    @property(Label)
    soldierName: Label

    @property(Label)
    level: Label


    @property(SpriteFrame)
    border1: SpriteFrame
    @property(SpriteFrame)
    border2: SpriteFrame
    @property(SpriteFrame)
    border3: SpriteFrame


    soldierData: Soldier
    offTime: number
    techData: Tech


    start() {
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this)
    }

    async setData(tech: Tech) {
        this.techData = tech
        this.soldierName.string = tech.name
        this.level.string = `${tech.level}/${tech.max}`


        let event = tech.getStudyEvent()
        if (event) {
            this.countDown(event)
            this.develop.string = ''
            this.level.node.parent.active = false
            this.border.spriteFrame = this.border2
        } else {
            this.develop.node.parent.active = false
            
            if (tech.level == 0) {
                this.border.spriteFrame = this.border3
            }

        }

        // if (tech.level > 0) {
        //     this.develop.node.parent.active = false
        // } else {

        // }

        this.cover.spriteFrame = await tech.getTechImg()
    }

    get deDesc():string{
        if(this.techData.level == 0){
            return '研发中'
        } else {
            return '强化中'
        }
    }

    countDown(event: Events) {
        this.unschedule(this.updateTime)
        if (!event) {
            return
        }

        let eStartTime = event.eStartTime
        this.offTime = eStartTime - Math.floor(UserInfo.instance.serverTime / 1000)
        if (this.offTime <= 0) {
            UserInfo.instance.refreshVillageDetail()
            return
        }

        this.develop.string = `${this.deDesc}\n${Utils.secondToStr(this.offTime)}`
        this.schedule(this.updateTime, 1, this.offTime - 1)
    }

    updateTime() {
        this.offTime--
        this.develop.string = `${this.deDesc}\n${Utils.secondToStr(this.offTime)}`

        if (this.offTime <= 0) {
            // 倒计时完成
            // 清除事件，刷新页面
            // this.soldierData.developComplete()
            UserInfo.instance.refreshVillageDetail()
            AudioManager.instance.playOneShot(Mp3Enum.research_complete)
        }
    }

    async onTouchEnd() {
        if(this.techData.type == TechType.Soldier) {
            this.toSoldier()
        }

        if(this.techData.type == TechType.Building) {
            this.toBuilding()
        }
        
    }

    async toSoldier(){    
        let event = this.techData.getStudyEvent()
        if (this.techData.level > 0) {
            if (event) {
                let ui = await UIManager.instance.openUI(PrefabEnum.Upgrading)
                ui.getComponent(Upgrading).setData(this.techData)
            } else {
                let ui = await UIManager.instance.openUI(PrefabEnum.UpgradeTech)
                ui.getComponent(UpgradeTech).setData(this.techData)
            }
        } else {
            if (event) {
                let ui = await UIManager.instance.openUI(PrefabEnum.Developling)
                ui.getComponent(Developing).setData(this.techData)
            } else {
                let ui = await UIManager.instance.openUI(PrefabEnum.DevelopTech)
                ui.getComponent(DevelopTech).setData(this.techData)
            }
        }
    }

    async toBuilding(){
        let event = this.techData.getStudyEvent()
        if (this.techData.level > 0) {
            if (event) {
                let ui = await UIManager.instance.openUI(PrefabEnum.UpgradingBuilding)
                ui.getComponent(UpgradingBuilding).setData(this.techData)
            } else {
                let ui = await UIManager.instance.openUI(PrefabEnum.UpgradeTechBuilding)
                ui.getComponent(UpgradeTechBuilding).setData(this.techData)
            }
        } else {
            if (event) {
                let ui = await UIManager.instance.openUI(PrefabEnum.Developling)
                ui.getComponent(Developing).setData(this.techData)
            } else {
                let ui = await UIManager.instance.openUI(PrefabEnum.DevelopTech)
                ui.getComponent(DevelopTech).setData(this.techData)
            }
        }
    }
}


